using System;
using System.Xml;

namespace BEGDC.CC.BL.Workflow
{
	/// <summary>
	/// Configuration information for CQMS Workflow
	/// </summary>
	public class Configuration : BEGDC.Utility.Configuration.Configuration
	{
		/// <summary>
		/// Constructor
		/// </summary>
		protected Configuration() : base()
		{
			this.process_id_cr = Convert.ToInt64(this["CRPDID"]);
			this.process_id_ocr = Convert.ToInt64(this["OCRPDID"]);
			this.process_id_outcr = Convert.ToInt64(this["OutCRPDID"]);
			this.process_id_cv = Convert.ToInt64(this["CVPDID"]);
			this.process_id_ev = Convert.ToInt64(this["EVPDID"]);
			this.process_id_ap = Convert.ToInt64(this["APPDID"]);
			this.process_id_ocv = Convert.ToInt64(this["OCVPDID"]);
			this.process_id_ebd = Convert.ToInt64(this["EBDPDID"]);//added by yuliang
			this.process_id_ecs = Convert.ToInt64(this["ESCPDID"]);//added by zhaosong
			this.process_id_ebdcv = Convert.ToInt64(this["EBDCVPDID"]);//added by zhaosong
			this.process_id_ecscv = Convert.ToInt64(this["ESCCVPDID"]);//added by zhaosong
			this.prostates_def_cr = LoadProcessStatesDefinition("CRProcessStates");
			this.prostates_def_ocr = LoadProcessStatesDefinition("OCRProcessStates");
			this.prostates_def_ebd = LoadProcessStatesDefinition("EBDProcessStates");
			this.prostates_def_ecs = LoadProcessStatesDefinition("ESCProcessStates");//added by zhaosong
			this.prostates_def_ebdcv = LoadProcessStatesDefinition("EBDCVProcessStates");//added by zhaosong
			this.prostates_def_ecscv = LoadProcessStatesDefinition("ESCCVProcessStates");//added by zhaosong
			this.prostates_def_outcr = LoadProcessStatesDefinition("OutCRProcessStates");
			this.prostates_def_cv = LoadProcessStatesDefinition("CVProcessStates");
			this.prostates_def_ev = LoadProcessStatesDefinition("EVProcessStates");
			this.prostates_def_ap = LoadProcessStatesDefinition("APProcessStates");
			this.prostates_def_ocv = LoadProcessStatesDefinition("OCVProcessStates");
			this.attachment_path = this["AttachmentPath"];
			this.LoadGroups();
		}

		/// <summary>
		/// The module name of configruation in configuration file.
		/// </summary>
		public override string ModuleName
		{
			get
			{
				return "BEGDC.CC.BL.Workflow";
			}
		}

		private long process_id_cr, process_id_cv, process_id_ev, process_id_ap, process_id_outcr, process_id_ocr, process_id_ocv,process_id_ebd,process_id_ecs,process_id_ebdcv,process_id_ecscv;
		private string attachment_path;

		private ProcessStateList prostates_def_cr, prostates_def_cv, prostates_def_ev, prostates_def_ap,prostates_def_outcr, prostates_def_ocr, prostates_def_ocv,prostates_def_ebd,prostates_def_ecs,prostates_def_ebdcv,prostates_def_ecscv;

		private string[] role_groups;

		private ProcessStateList LoadProcessStatesDefinition(string processName)
		{
			string filename = this[processName];			
			filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + filename;
			XmlDocument doc = new XmlDocument();
			try
			{
				doc.Load(filename);			
			}
			catch(Exception ex)
			{
				throw ex;
			}
			XmlNode root = doc.LastChild;
			
			ProcessStateList states = new ProcessStateList();

			for(int i=0; i<root.ChildNodes.Count; i++)
			{
				ProcessState ps = new ProcessState(root.ChildNodes[i]);
				states.Add(ps);
			}
			return states;
		}

		private void LoadGroups()
		{
			string groups = this["RoleGroups"];
			if ((groups == null) || (groups == string.Empty))
			{
				role_groups = new string[0];
			}
			else
			{
				role_groups = groups.Split(',');
			}
		}

		private static Configuration config = null;

		public static long PDCRID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_cr;
			}
		}

		public static long PDOCRID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_ocr;
			}
		}

		public static long PDOutCRID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_outcr;
			}
		}

		public static long PDCVID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_cv;
			}
		}

		public static long PDOCVID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_ocv;
			}
		}

		public static long PDEVID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_ev;
			}
		}

		public static long PDAPID
		{
			get
			{
				if( config == null )
					config = new Configuration();
				return config.process_id_ap;
			}
		}

		//added by yuliang
		public static long PDEBDID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_ebd;
			}
		}

		//added by zhaosong
		public static long PDESCID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_ecs;
			}
		}

		//added by zhaosong
		public static long PDEBDCVID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_ebdcv;
			}
		}

		//added by zhaosong
		public static long PDESCCVID
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.process_id_ecscv;
			}
		}

		public static string AttachmentPath
		{
			get
			{
				if( config == null )
					config = new Configuration();
				return config.attachment_path;
			}
		}

		/// <summary>
		/// The cq process definition information
		/// </summary>
		public static ProcessStateList CRProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_cr;
			}
		}

		public static ProcessStateList OCRProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ocr;
			}
		}
		public static ProcessStateList EBDProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ebd;
			}
		}
		//added by zhaosong
		public static ProcessStateList ESCProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ecs;
			}
		}
		//added by zhaosong
		public static ProcessStateList EBDCVProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ebdcv;
			}
		}
		//added by zhaosong
		public static ProcessStateList ESCCVProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ecscv;
			}
		}

		public static ProcessStateList OutCRProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_outcr;
			}
		}

		public static ProcessStateList CVProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_cv;
			}
		}

		public static ProcessStateList OCVProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ocv;
			}
		}

		public static ProcessStateList EVProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ev;
			}
		}

		public static ProcessStateList APProcessDefinition
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.prostates_def_ap;
			}
		}

		private static bool is_setstates = false;
		internal static bool IsSetStates
		{
			get
			{
				return is_setstates;
			}
			set
			{
				is_setstates = value;
			}
		}

		/// <summary>
		/// The process definition information
		/// </summary>
		public static string[] RoleGroups
		{
			get
			{
				if (config == null)
					config = new Configuration();
				return config.role_groups;
			}
		}

		public static ProcessStateList GetProcessStateList(string type)
		{
			switch(type)
			{
				case WorkflowTypes.CR:
					return Configuration.CRProcessDefinition;
				case WorkflowTypes.OCR:
					return Configuration.OCRProcessDefinition;
				case WorkflowTypes.EBD:
					return Configuration.EBDProcessDefinition;
				//added by zhaosong
				case WorkflowTypes.ESC:
					return Configuration.ESCProcessDefinition;
				case WorkflowTypes.EBDCV:
					return Configuration.EBDCVProcessDefinition;
				case WorkflowTypes.ESCCV:
					return Configuration.ESCCVProcessDefinition;
				case WorkflowTypes.CO:
					return Configuration.OutCRProcessDefinition;
				case WorkflowTypes.CV:
					return Configuration.CVProcessDefinition;
				case WorkflowTypes.EV:
					return Configuration.EVProcessDefinition;
				case WorkflowTypes.AP:
					return Configuration.APProcessDefinition;
				case WorkflowTypes.OCV:
					return Configuration.OCVProcessDefinition;
				
			}
			return null;
		}

		public static long GetProcessDefinitionID(string type)
		{
			switch(type)
			{
				case WorkflowTypes.CR:
					return Configuration.PDCRID;
				case WorkflowTypes.OCR:
					return Configuration.PDOCRID;
				case WorkflowTypes.CO:
					return Configuration.PDOutCRID;
				case WorkflowTypes.CV:
					return Configuration.PDCVID;
				case WorkflowTypes.EV:
					return Configuration.PDEVID;
				case WorkflowTypes.AP:
					return Configuration.PDAPID;
				case WorkflowTypes.OCV:
					return Configuration.PDOCVID;
				case WorkflowTypes.EBD:
					return Configuration.PDEBDID;
				case WorkflowTypes.ESC:
					return Configuration.PDESCID;
				case WorkflowTypes.EBDCV:
					return Configuration.PDEBDCVID;
				case WorkflowTypes.ESCCV:
					return Configuration.PDESCCVID;
			}
			return -1;
		}
	}
}
